<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class CancelDeductionModel extends Model
{
    protected $table = 'edu_cancel_deduction';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getCancelDeductionLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
        $where = [];
        if (isset($param['student_name']) && '' !== $param['student_name']) {  //学生姓名
            $where['student_name'] = ['like', '%'.$param['student_name'].'%'];
        }


        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }

        //订单编号
        if (isset($param['contract_number']) && '' !== $param['contract_number']) {
            $ids = '';
            $infos_result = $StudentSubjectInfosClassModel->where(['contract_number' => $param['contract_number']])->select();
            foreach ($infos_result as $key => $value){
                $ids = $ids.$value['id'] . ',';
            }
            $ids = substr($ids,0,strlen($ids)-1);
            $where['infos_id'] = array('in',$ids);
        }


        //课程信息
        $CourseModel = new CourseModel();
        $course_result =$CourseModel->select();
        $course_data = [];
        foreach ($course_result as $key => $value){
            $course_data[$value['id'].''] = $value['course_name'];
        }
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
        $listData = self::where($where)
            ->order('create_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();

        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $res =  $StudentSubjectInfosClassModel->get(['id' => $item['infos_id']]);
                if($res){
                    $item['course_text'] =  $course_data[$res['course_id'].'']; //课程名称
                }else{
                    $item['course_text'] = '';
                }
                $res_grades = GradesDetailsModel::get(['id' => $res['class_subject_id']]);
                $item['grades_name'] =$res_grades['grades_name']; //班级名称
                $item['student_name'] = $res['student_name'];  //学员姓名
                if($res['contract_number'] == '---'){
                    $item['contract_number'] = '-'; //订单编号
                }else{
                    $item['contract_number'] = $res['contract_number']; //订单编号
                }
                $item['duration'] = $res['duration'];//时长
                $item['teacher'] = $res_grades['teacher'];//任课老师
                $item['operatorer'] =  '<span  style="color:#01AAED">'.$item['operatorer'].'</span>'; //操作人
                $item['headmaster'] = $res_grades['headmaster']; //班主任
                $item['amount_deducted'] = '<span  style="color:#FF5722">'.($item['amount_deducted'] / 100).'</span>';
                $item['eliminated_nums'] = '<span  style="color:#01AAED">'.$res['eliminated_nums'].'</span>';
                $item['status_text'] = '<span  style="color:#FF5722; font-weight: bold;">已取消</span>';
                $item['cancel_reason'] = '<span  style="color:#01AAED">'.$item['cancel_reason'].'</span>';
            }
        }

        return $listData;
    }


}
